//
//  IMLoginReceiver.m
//  IMChezhil
//
//  Created by zhangliucheng on 15/7/24.
//  Copyright (c) 2015年 CheZhil. All rights reserved.
//

#import "IMLoginReceiver.h"

#define LoginNotification @"LoginNotification"

@interface IMLoginReceiver ()

@property (copy, nonatomic) OnLoginBlock block;

@end

@implementation IMLoginReceiver

- (instancetype)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiver:) name:LoginNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:LoginNotification object:Nil];
    NSLog(@"%s", __FUNCTION__);
}

- (void)onReceiver:(NSNotification *)notification {
    NSString *content = notification.object;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[content dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:Nil];
    if ([[dict objectForKey:@"status"] boolValue]) {
        self.block(YES);
        loginReceiver = Nil;
    } else {
        self.block(NO);
    }
}

static IMLoginReceiver *loginReceiver;
+ (void)onRegisterNodification:(OnLoginBlock)block {
    loginReceiver = Nil;
    loginReceiver = [IMLoginReceiver new];
    loginReceiver.block = block;
}

+ (void)postLoginNotification:(NSString *)content {
    [[NSNotificationCenter defaultCenter] postNotificationName:LoginNotification object:content];
}
@end
